<?php
class Empeval_GeneratePassword extends Core_Sender {
	
	const PASSWORD_RESET_TITLE = 'System premiowy i ocena pracownicza VGP - hasło użytkownika.';
	
	public function __construct() {
		$config = Empeval_Config::getInstance()->mailBoxs->notifications;
		parent::__construct($config);
	}
	
	public function start() {
		$users = EmpevalUsersQuery::create()
		->filterByDeleted(null, Criteria::ISNULL)
		->filterByActive(true)
		->filterByPassword(null, Criteria::ISNULL)
		->filterByEmail(null, Criteria::ISNOTNULL)
		->findOne();
		
		$passwords = array();
		foreach ($users as $user) {
			$password = $user->generatePassword();
			$user->save();
			$passwords[] = array($user->getEmail(), $password);
			//$this->sendMail($user, $password);
		}
		file_put_contents(dirname(__FILE__), Core_Tools::ArrayToCSV($passwords));
	}
	
	public function sendMail(EmpevalUsers $user, $password) {
		$viewDir = Zend_Controller_Front::getInstance()->getModuleDirectory('empeval') . '/views/scripts/partials/';
		$view = new Zend_View();
		$view->setScriptPath($viewDir);
		$view->password = $password;
		$view->email = $user->getEmail();
		$body = $view->render('password.phtml');
		
    $this->phpMailer->ClearAddresses();
    //$this->phpMailer->AddAddress($user->getEmail());
    $this->phpMailer->AddAddress('jakub.stepien@gmind.pl');
    $this->phpMailer->IsHTML(true);
    $this->phpMailer->Subject = self::PASSWORD_RESET_TITLE;
    $this->phpMailer->Body = $body;
    $this->phpMailer->AltBody = strip_tags(preg_replace('#<br\s*/?>#i', "\n", $body));
    $this->phpMailer->Send();
	}
	
}